package com.xinhua.erp.service;

import antlr.StringUtils;
import com.xinhua.erp.pojo.*;
import com.xinhua.erp.repo.InventoryRepo;
import com.xinhua.erp.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Map;

import static antlr.StringUtils.*;
import static org.apache.logging.log4j.util.Strings.isBlank;

@Service
@Transactional
public class InventoryService {

    @Autowired
    private InventoryRepo inventoryRepo;

    @Autowired
    private IdWorker idWorker;

    public Result findAllByStockhouseId(Long stockhouseId,int page ,int size){

        Pageable pageable = PageRequest.of(page-1, size);

        Page<Inventory>inventories=inventoryRepo.findAllByStockhouseId(stockhouseId,pageable);

        PageResult<Inventory> pageResult = new PageResult<>(inventories.getTotalElements(),inventories.getContent());

        return Result.OK(pageResult);

    }

    public Result Search(Map searchMap, int page, int size){
        Pageable pageable= PageRequest.of(page-1, size);
        Page<Inventory> inventories= inventoryRepo.findAll(createSpecification(searchMap),pageable);
        return Result.OK(new PageResult<>(inventories.getTotalElements(),inventories.getContent()));
    }

    private Specification<Inventory> createSpecification(Map searchMap) {

        return new Specification<Inventory>(){
            @Override
            public Predicate toPredicate(Root<Inventory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList=new ArrayList<>();
                if(!isBlank((String)searchMap.get("stockhouseName"))){

                    predicateList.add(criteriaBuilder.like(root.get("stockhouseName").as(String.class),"%"+searchMap.get("stockhouseName")+"%"));

                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        }

        public Result findBystockhouseIdAndproductId(long stockhouseId, long productId){

            return Result.OK(inventoryRepo.findByStockhouseIdAndProductId(stockhouseId, productId));
        }

        public Result save(Inventory inventory) {

        inventory.setId(idWorker.nextId());

        inventoryRepo.save(inventory);

        return Result.OK();
    }

    public Result updateById(long id,Inventory inventory) {
        inventory.setId(id);
        inventoryRepo.save(inventory);

        return Result.OK();
    }

    public Result deleteById(long id) {

        inventoryRepo.deleteById(id);

        return Result.OK();
    }
    }



